import pandas as pd
import numpy as np
# axis (合并方向):axis=0是预设值，因此未设定任何参数时，函数默认axis=0。
a_01 = pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'])
a_02 = pd.DataFrame(np.ones((3,4))*1,columns=['a','b','c','d'])
a_03 = pd.DataFrame(np.ones((3,4))*2,columns=['a','b','c','d'])
a_04 = pd.DataFrame(np.ones((3,4))*3,columns=['a','b','c','d'])
#concat纵向合并
result = pd.concat([a_01,a_02,a_03,a_04],axis=0)
print(result)
# 观察会发现结果的index是0, 1, 2, 0, 1, 2, 0, 1, 2
# ignore_index (重置 index),即重新排序
result = pd.concat([a_01,a_02,a_03,a_04],axis=0,ignore_index=True)
print(result)
print(np.ones((3,4))*0)
"""
join='outer'为预设值，因此未设定任何参数时，函数默认join='outer'。
此方式是依照column来做纵向合并，有相同的column上下合并在一起，
其他独自的column个自成列，原本没有值的位置皆以NaN填充。
"""
d_01 = pd.DataFrame(np.ones((3,4))*0,index=[1,2,3],columns=['a','b','c','d'])
d_02 = pd.DataFrame(np.ones((3,4)),index=[2,3,4],columns=['e','b','c','n'])
#纵向"外"合并
res = pd.concat([d_01,d_02],axis=0,join='outer')
print(res)
"""
     a    b    c    d    e    n
1  0.0  0.0  0.0  0.0  NaN  NaN
2  0.0  0.0  0.0  0.0  NaN  NaN
3  0.0  0.0  0.0  0.0  NaN  NaN
1  NaN  1.0  1.0  NaN  1.0  1.0
2  NaN  1.0  1.0  NaN  1.0  1.0
3  NaN  1.0  1.0  NaN  1.0  1.0
"""
# 使用join='inner'，只有相同的column合并在一起，其他的会被抛弃
res_02 = pd.concat([d_01,d_02],axis=0,ignore_index=True,join='inner')
print(res_02)
"""
     b    c
0  0.0  0.0
1  0.0  0.0
2  0.0  0.0
3  1.0  1.0
4  1.0  1.0
5  1.0  1.0
"""
# join_axes (依照 axes 合并):join_axes在新版中取消了
res_03 = pd.concat([d_01,d_02.reindex_like(d_01)],axis=1)
print(res_03)
# append (添加数据)
# append只有纵向合并，没有横向合并
s1 = pd.Series([1,2,3,4],index=['a','b','c','e'])
res_04 = d_01.append(d_02,ignore_index=True)
print(res_04)
print(d_01.append(s1,ignore_index=True))

"""
Pandas 合并 merge:
pandas中的merge和concat类似,但主要是用于两组有key column的数据,统一索引的数据. 通常也被用在Database的处理当中.
"""
# left = pd.DataFrame({'key':['K0','K1','K2','K3'],
#                      'A':['A0','A1','A2','A3'],
#                      'B':['b0','b1','b2','b3']
#                      })
# right = pd.DataFrame({'key':['K0','K1','K2','K4'],
#                      'C':['C0','C1','C2','C3'],
#                      'D':['D0','D1','D2','D3']
#                      })
# print(left,'\n',
#       right)
# # 依据key column合并，并打印出
# res = pd.merge(left,right,on='key')
# print(res)

# 依据两组key进行合并
left = pd.DataFrame({'key1':['K0','K1','K2','K3'],
                     'key2':['K0','K2','K2','K3'],
                     'A':['A0','A1','A2','A3'],
                     'B':['b0','b1','b2','b3']
                     })
right = pd.DataFrame({'key1':['K0','K1','K2','K4'],
                     'key2':['K0','K0','K2','K4'],
                     'C':['C0','C1','C2','C3'],
                     'D':['D0','D1','D2','D3']
                     })
res_01 = pd.merge(left,right,on=['key1','key2'],how='inner')
print("1、使用合并方法inner:\n",res_01)

res_02 = pd.merge(left,right,on=['key1','key2'],how='outer')
print("2、使用合并方法outer:\n",res_02)

res_03 = pd.merge(left,right,on=['key1','key2'],how='left')
print("3、使用合并方法left:\n",res_03)

res_04 = pd.merge(left,right,on=['key1','key2'],how='right')
print("4、使用合并方法right:\n",res_04)